package cn.wolfcode.shop.service.impl;

import cn.wolfcode.shop.common.BusinessException;
import cn.wolfcode.shop.domain.OrderInfo;
import cn.wolfcode.shop.mapper.OrderInfoMapper;
import cn.wolfcode.shop.service.IOrderInfoService;
import cn.wolfcode.shop.service.ISeckillGoodService;
import cn.wolfcode.shop.service.ISeckillOrderService;
import cn.wolfcode.shop.util.IdGenerateUtil;
import cn.wolfcode.shop.vo.SeckillGoodVo;
import cn.wolfcode.shop.web.result.SeckillServerCodeMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class OrderInfoServiceImpl implements IOrderInfoService{

    @Autowired
    private ISeckillGoodService seckillGoodService;

    @Autowired
    private ISeckillOrderService seckillOrderService;

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Transactional
    public String seckill(Long userId, Long seckillGoodId) {
        // 库存减1
        int stock = seckillGoodService.reduceStock(seckillGoodId);
        if (stock == 0){
            throw new BusinessException(SeckillServerCodeMsg.SECKILL_OVER);
        }
        // 创建订单
        OrderInfo orderInfo = this.createOrderInfo(userId, seckillGoodId);
        // 创建秒杀订单
        seckillOrderService.createSeckillOrder(userId, seckillGoodId,orderInfo.getOrderNo());
        return orderInfo.getOrderNo();
    }

    private OrderInfo createOrderInfo(Long userId, Long seckillGoodId) {
        SeckillGoodVo vo = seckillGoodService.find(seckillGoodId);
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setCreateDate(new Date());
        orderInfo.setGoodCount(1);
        orderInfo.setGoodId(vo.getGoodId());
        orderInfo.setGoodImg(vo.getGoodImg());
        orderInfo.setGoodName(vo.getGoodName());
        orderInfo.setGoodPrice(vo.getGoodPrice());
        orderInfo.setSeckillPrice(vo.getSeckillPrice());
        orderInfo.setUserId(userId);
        orderInfo.setOrderNo(IdGenerateUtil.get().nextId()+"");
        orderInfoMapper.insert(orderInfo);
        return orderInfo;
    }

    public OrderInfo find(String orderNo) {

        return orderInfoMapper.find(orderNo);
    }

    @Transactional
    public void cancelTimeOutOrder(String orderNo, Long seckillGoodId) {
        OrderInfo orderInfo = orderInfoMapper.find(orderNo);
        // 用户订单已经支付状态，用户手动取消
        if (orderInfo.getStatus().equals(OrderInfo.STATUS_ACCOUNT_PAID )|| orderInfo.getStatus().equals(OrderInfo.STATUS_CANCEL)){
            return;
        }
        // 能来到这里说明没有支付，取消订单
        int count = orderInfoMapper.updateStatus(orderNo,OrderInfo.STATUS_TIMEOUT);
        // 刚好在15分钟，在操作这个超时取消订单逻辑的时候，付款了或者手动取消订单
        if (count == 0){
            throw new BusinessException(SeckillServerCodeMsg.CANCEL_TIMEOUT_QUEUE_ERROR);
        }
        // 说明我们已经修改订单状态为3（超时取消）
        // 真实库存+1
        seckillGoodService.incrStock(seckillGoodId);
        // 同步预库存，取消本地标识
        seckillGoodService.syncStock(seckillGoodId);
    }

    public int updateOrder(String orderNo, Integer status) {
        OrderInfo orderInfo = orderInfoMapper.find(orderNo);
        if (!orderInfo.getStatus().equals(OrderInfo.STATUS_ARREARAGE)){
            throw new BusinessException(SeckillServerCodeMsg.PAY_ERROR);
        }
        int count = orderInfoMapper.updatePayState(orderNo, status);
        if (count == 0){
            throw new BusinessException(SeckillServerCodeMsg.PAY_ERROR);
        }
        return count;
    }
}
